#include "ccd290_config.h"

int init_ccd290_config_from_file( ccd290_config *cfg, char *inifile ){

    init_ccd290_config_to_defaults( cfg );

    // extract configuraion parameters using inifile
    dictionary *ini;

    ini = iniparser_load(inifile);

#ifdef _DEBUG_INI_
    char ini_tmp[128] = "ini_debug.ini";
    printf("== dump ini contents into : %s\n", ini_tmp);
    FILE *fp = fopen(ini_tmp,"w");
    iniparser_dump_ini(ini, fp);
    printf("== finished debug iniparser ==\n");
#endif

    // Raw data type
    cfg->raw_dtype_is_float = iniparser_getint(ini, "CCD:raw_dtype_is_float", 0);

    // Image size:
    cfg->N_ROW              = iniparser_getint(ini, "CCD:N_ROW", 1200);
    cfg->N_COL              = iniparser_getint(ini, "CCD:N_COL", 2148);
    cfg->HAS_TAG            = iniparser_getint(ini, "CCD:HAS_TAG", 0);
    cfg->SAMP_NUM_PER_REG   = iniparser_getint(ini, "CCD:SAMP_NUM_PER_REG", 1);

    cfg->LEN = cfg->N_ROW * (cfg->N_COL + cfg->HAS_TAG) * cfg->SAMP_NUM_PER_REG;

    // CDS:
    cfg->cds_offset     = iniparser_getint(ini, "CDS:cds_offset", 0);
	cfg->cds_weight	    = iniparser_getint(ini, "CDS:cds_weight", 10);
	cfg->idx_offset     = iniparser_getint(ini, "CDS:idx_offset", 10);
	
	// FITS
	cfg->include_line_num  = iniparser_getint(ini, "FITS:include_line_num", 0);
    cfg->use_csst_format   = iniparser_getint(ini, "FITS:use_csst_format", 0);
    cfg->use_mock_scan     = iniparser_getint(ini, "FITS:use_mock_scan", 0);

    cfg->use_uint16_format = iniparser_getint(ini, "FITS:use_uint16_format", 1);

    cfg->remove_preover_scan = iniparser_getint(ini, "FITS:remove_preover_scan", 0);

    iniparser_freedict(ini);

    return 0;
}

int init_ccd290_config_to_defaults( ccd290_config* cfg ){

    // Raw data type
    // 0: 二进制数据对应的数据为32bits无符号整数
    // 1: 二进制数据对应的数据为32bits浮点数
    cfg->raw_dtype_is_float = 0;

    // Image size:
    cfg->N_ROW              = 1200;
    cfg->N_COL              = 2148;
    cfg->HAS_TAG            = 0;
    cfg->SAMP_NUM_PER_REG   = 1;
    cfg->LEN                = cfg->N_ROW * (cfg->N_COL + cfg->HAS_TAG) * cfg->SAMP_NUM_PER_REG;

    // CDS:
    cfg->cds_offset = 0;
    cfg->idx_offset = 0;
    cfg->cds_weight	= 10;
    
    // FITS
    cfg->include_line_num   = 0;
    cfg->use_csst_format    = 0;
    cfg->use_mock_scan      = 0;
    cfg->use_uint16_format  = 1;
    cfg->remove_preover_scan= 0;

    return 0;
} 
